package com.ifood.framework.security.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.handlers.StrictFill;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.ifood.common.core.domain.model.LoginUser;
import com.ifood.common.enums.DelFlagEnum;
import com.ifood.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.security.Security;
import java.util.Date;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author donghc
 * @ClassName MyMetaObjectHandler
 * @description: TODO
 * @date 2024年01月08日
 * @version: 1.0
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public boolean openInsertFill() {
        return MetaObjectHandler.super.openInsertFill();
    }

    @Override
    public boolean openUpdateFill() {
        return MetaObjectHandler.super.openUpdateFill();
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        // LoginUser user = SecurityUtils.getLoginUser();
        if (metaObject.hasGetter("updateTime")) {
            metaObject.setValue("updateTime", null);
            this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        }

        if (metaObject.hasGetter("createTime")) {
            metaObject.setValue("createTime", null);
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        }
        if (metaObject.hasGetter("delFlag")) {
            metaObject.setValue("delFlag", null);
            this.strictInsertFill(metaObject, "delFlag", String.class, DelFlagEnum.normal.getCode());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter("updateTime")) {
            metaObject.setValue("updateTime", null);
            this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        }
    }

    @Override
    public MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
        return MetaObjectHandler.super.setFieldValByName(fieldName, fieldVal, metaObject);
    }

    @Override
    public Object getFieldValByName(String fieldName, MetaObject metaObject) {
        return MetaObjectHandler.super.getFieldValByName(fieldName, metaObject);
    }

    @Override
    public TableInfo findTableInfo(MetaObject metaObject) {
        return MetaObjectHandler.super.findTableInfo(metaObject);
    }


    @Override
    public MetaObjectHandler strictInsertFill(TableInfo tableInfo, MetaObject metaObject, List<StrictFill<?, ?>> strictFills) {
        return MetaObjectHandler.super.strictInsertFill(tableInfo, metaObject, strictFills);
    }


    @Override
    public MetaObjectHandler strictUpdateFill(TableInfo tableInfo, MetaObject metaObject, List<StrictFill<?, ?>> strictFills) {
        return MetaObjectHandler.super.strictUpdateFill(tableInfo, metaObject, strictFills);
    }

    @Override
    public MetaObjectHandler strictFill(boolean insertFill, TableInfo tableInfo, MetaObject metaObject, List<StrictFill<?, ?>> strictFills) {
        return MetaObjectHandler.super.strictFill(insertFill, tableInfo, metaObject, strictFills);
    }

    @Override
    public MetaObjectHandler fillStrategy(MetaObject metaObject, String fieldName, Object fieldVal) {
        return MetaObjectHandler.super.fillStrategy(metaObject, fieldName, fieldVal);
    }

    @Override
    public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
        return MetaObjectHandler.super.strictFillStrategy(metaObject, fieldName, fieldVal);
    }
}
